//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/.
// 			VisualNovelToolkit		     /_/_/_/_/_/_/_/_/_/.
// Copyright ©2013 - Sol-tribe.	/_/_/_/_/_/_/_/_/_/.
//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/.

using UnityEngine;
using UnityEditor;
using System.Collections;
using ViNoToolkit;

public class ViNoToolbar : EditorWindow {
	static public int m_Selected = 0;
	static public string[] m_MenuItems = {  "Scenario" , "Nodes" , "Templates" };
		
	static public ScenarioNode m_CurrScenarioNode;
	static public string m_ScenarioName = "";
	
	static public bool m_StartAndPlay = true;
	public TextAsset scenarioScript;

	static public GameObject m_AddedObject;
	
	public GameObject m_CurrSelected;

	static private DrawScenarioTab m_DrawScenario;
	static private DrawScenarioTab drawScenario{
		get{
			if (m_DrawScenario == null) {
					m_DrawScenario = new DrawScenarioTab ();
			}
			return m_DrawScenario;
		}
	}
			
	 [MenuItem ("Window/ViNo/Toolbar")]
    static void Init() {		
      	EditorWindow.GetWindow (typeof (ViNoToolbar));		
    }		
		
	void OnSelectionChange( ){
		Repaint();
	}

	void OnGUI(){						
		float space = ViNoToolUtil.kToolbarHeight + 5f;
		GUICommon.DrawLineSpace( 5f , space );		
		
		m_Selected = GUILayout.Toolbar( m_Selected , m_MenuItems , GUILayout.Height ( ViNoToolUtil.kToolbarHeight ));		
		string selectedStr = m_MenuItems[ m_Selected ];
		switch( selectedStr ){
			case "Scenario":	drawScenario.Draw( this );			break;			
			case "Nodes":		DrawViNodesTab.Draw();				break;
			case "Samples":		DrawSamplesTab.Draw();				break;			
			case "Templates":	DrawTemplatesTab.Draw();			break;
		}
	}
	
	static public void SlideSelectionObjects( float toX , float fromX , float duration ){
			float localY = Selection.activeGameObject.transform.localPosition.y;								
			float localZ = Selection.activeGameObject.transform.localPosition.z;	
	
			AnimationNode animNode = GetAnimationNodeTempl( AnimationType.MOVE_TO , duration );
			animNode.amountX = toX;
			animNode.amountY = localY;
			animNode.amountZ = localZ;
	
			animNode.fromAmountX = fromX;// = new Vector3( -700f , 0f , z );
			animNode.fromAmountY = localY;
			animNode.fromAmountZ = localZ;
	
			animNode.toggleFromAmount = true;
			animNode.Preview();
	}
	
	static public AnimationNode GetAnimationNodeTempl(  AnimationType tp , float duration  ){
		string path =  ViNoToolUtil.GetAssetDataPath() + "Templates/AnimationNode.prefab";
		GameObject animObj = AssetDatabase.LoadAssetAtPath( path , typeof( GameObject) ) as GameObject;				
		AnimationNode animNode = animObj.GetComponent<AnimationNode>();				
		animNode.animationType = tp;
		animNode.animTarget = Selection.activeGameObject;
		animNode.duration = duration;					
		return animNode;		
	}
		
	static public GameObject ImportExampleCharacter( string path , string parentName ){
		GameObject obj = AssetDatabase.LoadAssetAtPath( path , typeof( GameObject) ) as GameObject;
		
		GameObject clone = GameObject.Instantiate( obj ) as GameObject;
		ViNoGOExtensions.StripGameObjectName( clone , "(Clone)" , "" );
		
		Vector3 originalPos = clone.transform.localPosition;		
		Quaternion originalRot = clone.transform.localRotation;		
		Vector3 originalScale = clone.transform.localScale;		
		
		GameObject character = GameObject.Find ( parentName );
		if( character != null ){
			clone.transform.parent = character.transform;
			clone.transform.localPosition = originalPos;
			clone.transform.localRotation = originalRot;
			clone.transform.localScale = originalScale;
		}
		return clone;
	}

}
